var handle = null;

function InitHandle(visible)
{
	if (visible == null)
	{
		visible = true;
	}

	handle = WScript.CreateObject("InternetExplorer.Application", "WM_");
	handle.Silent = true;

	if (visible == true)
	{
		handle.Visible = true;
	}

	return handle;
}

function WaitForDocumentComplete(handle)
{
	do
	{
		WScript.Sleep(10);
	} while (handle.Busy || handle.ReadyState != 4);
}

function GetDocument(handle, url)
{
	if (url == null)
	{
		url = "about:blank";
	}

	if (url == handle.LocationURL)
	{
		handle.Refresh();
	}
	else
	{
		handle.navigate(url);
	}
	
	WaitForDocumentComplete(handle);
		
	return handle.document;
}

function quit(msg)
{
	for (var n = 0; n < handles.length; n++)
	{
		if (handles[n] != null)
		{
			handles[n].Quit();
			handles[n] = null;
		}
	}

	if (msg != null && msg != "")
	{
		WScript.Echo(msg);
	}

	WScript.Quit();
}

function setcookie(document, name, value, expireHours, domain, path)
{
	value = escape(value);
	if (expireHours != null && expireHours != 0)
	{
		var date=new Date();
		date.setTime(date.getTime()+expireHours*3600*1000);
		date = date.toGMTString();
		if (domain != null && path != null)
			document.cookie = name + "=" + value + "; domain=" + domain + "; path=" + path + "; expires=" + date;
		else
			document.cookie = name + "=" + value + "; expires=" + date;
	}
	else
	{
		if (domain != null && path != null)
			document.cookie = name + "=" + value + "; domain=" + domain + "; path=" + path;
		else
			document.cookie = name + "=" + value;
	}
}

function emptycookie(document, domain)
{
	var array = document.cookie.split("; ");
	for (n = 0; n < array.length; n++)
	{
		var index = array[n].indexOf("=");
		var name;
		
		if (index != -1)
			name = array[n].substring(0, index);
		else
			name = array[n];

		if (domain == null)
			setcookie(document, name, "", -1);
		else
			setcookie(document, name, "", -1, domain, "/");
	}
}

function query_cookie_value(name)
{
	var cookie = getdocument(gethandle(true, true)).cookie;
	var value = "";
	var array = cookie.split("; ");

	for (var n = 0; array[n] != null && array[n] != ""; n++)
	{
		if (array[n].indexOf(name) != -1)
		{
			value = array[n].substring(array[n].indexOf("=") + 1);
			break;
		}
	}

	return value;
}

function hook()
{
	var fso = new ActiveXObject("Scripting.FileSystemObject");
	var CurrentDir = fso.GetAbsolutePathName(".");
	var script=document.createElement("script");
	script.src= CurrentDir + "\\hook.js";
	document.appendChild(script);
}	

/* callback function
function WM0_OnQuit()
{
	// quit();
}

function WM0_DocumentComplete()
{
	;
}
*/
